<template>
  <div class="mod-user">
    <el-form :model="query" :inline="true" @keyup.enter.native="searchGrade()">
      <el-form-item>
        <el-select
          v-model="query.schoolType"
          filterable
          clearable
          @change="changeSchoolTypeForQuery"
          placeholder="请选择行业类型">
          <el-option
            v-for="item in schoolTypeList"
            :key="item.id"
            :label="item.value"
            :value="item.code">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button
          type="success"
          icon="el-icon-search"
          v-if="hasPermission(['system:ent:list'])"
          @click="searchGrade()">查询</el-button>

        <el-button type="primary" v-if="hasPermission(['system:ent:save'])"  @click="addGrade">新增</el-button>
        <!--<el-button @click="batchDelete" type="danger">批量删除</el-button>-->
      </el-form-item>
    </el-form>
    <el-table
      :data="gradeInfoList"
      border
      @select="checkData"
      @selection-change="handleSelectionChange"
      v-loading="loading"
      style="width: 100%;">

      <el-table-column
        type="selection"
        header-align="center"
        align="center"
        width="50">
      </el-table-column>

      <el-table-column
        align="center"
        label="企业名称">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.name}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="所属地区">
        <template slot-scope="scope">
          <el-tag size="small">{{areaCodeDict[scope.row.areaCode]}}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        label="行业类型">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.industryId | getSchoolTypeName}}</el-tag>
        </template>
      </el-table-column>


      <el-table-column
        prop="createTime"
        align="center"
        width="180"
        label="创建时间">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.createTime }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        prop="auditTime"
        align="center"
        width="180"
        label="修改时间">
        <template slot-scope="scope">
          <el-tag size="small">{{scope.row.auditTime }}</el-tag>
        </template>
      </el-table-column>

      <el-table-column
        align="center"
        width="250"
        label="操作">
        <template slot-scope="scope">
          <el-button
            size="small"
            type="warning"
            @click="staffInfo(scope.row)">查看员工</el-button>
          <el-button
            size="small"
            type="warning"
            v-if="hasPermission(['system:ent:update'])"
            @click="updateGradeInfo(scope.row)">修改</el-button>
          <el-button
            size="small"
            type="danger"
            v-if="hasPermission(['system:ent:deleteById'])"
            @click="deleteGradeInfo(scope.row)"
            >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-dialog
      :title="'查看员工'"
      :close-on-click-modal="false"
      :visible.sync="satffFlag">


      <el-table
        :data="studentList"
        border
        @select="checkData"
        @selection-change="handleSelectionChange"
        v-loading="loading"
        style="width: 100%;">

        <el-table-column
          prop="name"
          width="100"
          align="center"
          label="姓名">
        </el-table-column>

        <el-table-column
          align="center"
          label="职工头像">
          <template slot-scope="scope">
            <img v-if="scope.row.headImg" style="width: 40px; height: 40px;" :src="fileUrl + scope.row.headImg"/>
          </template>
        </el-table-column>

        <el-table-column
          prop="sex"
          width="80"
          align="center"
          label="性别">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.sex === 1" size="small">男</el-tag>
            <el-tag v-else size="small">女</el-tag>
          </template>
        </el-table-column>

        <el-table-column
          prop="age"
          width="50"
          align="center"
          label="年龄">
        </el-table-column>



        <el-table-column
          prop="mobile"
          align="center"
          min-width="100"
          label="联系电话">
        </el-table-column>

        <el-table-column
          prop="address"
          align="center"
          width="100"
          :show-overflow-tooltip="true"
          label="住址">
        </el-table-column>

      </el-table>

      <span slot="footer"  class="dialog-footer">
        <el-button @click="satffFlag = false">取消</el-button>
        <el-button type="primary"  @click="satffFlag = false">确定</el-button>
      </span>
    </el-dialog>

    <el-dialog
      :title="!form.id ? '新增企业' : '编辑企业'"
      :close-on-click-modal="false"
      :visible.sync="showFlag">


      <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
        <el-form-item label="企业名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="所属地区" prop="areaCode">
          <el-select
            v-model="form.areaCode"
            filterable
            clearable
            placeholder="请选择">
            <el-option
              v-for="item in areaCodeList"
              :key="item.code"
              :label="item.name"
              :value="item.code">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="行业类型" placeholder="行业类型" prop="industryId">
          <el-select
            @change="changeSchoolType"
            v-model="form.industryId"
            filterable
            clearable
            placeholder="请选择">
            <el-option
              v-for="item in schoolTypeList"
              :key="item.id"
              :label="item.value"
              :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>

      </el-form>
      <span slot="footer"  class="dialog-footer">
        <el-button @click="showFlag = false">取消</el-button>
        <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
      </span>
    </el-dialog>

    <el-pagination
      background
      @size-change="sizeChangeHandler"
      @current-change="currentChangeHandler"
      :current-page="currentPage"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="pageSize"
      :total="totalCount"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>
  </div>
</template>

<script>
  let that = null
  // eslint-disable-next-line import/first
  import {getDictValueByType} from '@/api/dict'
  // eslint-disable-next-line import/first
  import {getEnterpriseList, getAreaCodeList} from '@/api/enterprise'
  export default {
    name: 'enterprise',
    data () {
      return {
        studentList: [],
        currentPage: 1,
        pageSize: 10,
        totalCount: 0,
        satffFlag: false,
        loading: true,
        showFlag: false,
        schoolTypeList: [],
        areaCodeList: [],
        gradeInfoList: [],
        form: {
          name: '',
          schoolType: ''
        },
        query: {
          schoolType: '',
          name: ''
        },
        rules: {

          name: [
            {required: true, message: '请输入企业名称', trigger: 'blur'}
          ],
          areaCode: [
            {required: true, message: '请选择所属地区', trigger: 'blur'}
          ],
          industryId: [
            {required: true, message: '请选择行业类型', trigger: 'blur'}
          ]
        }
      }
    },

    computed: {
      areaCodeDict () {
        const result = {}
        this.areaCodeList.forEach((row) => {
          result[row.code] = row.name
        })
        return result
      }
    },

    beforeCreate () {
      that = this
    },

    mounted () {
      getDictValueByType('industry_type').then(response => {
        this.schoolTypeList = response.data.data
      })
      this.getList()
    },

    methods: {

      getList () {
        let params = {
          pageNumber: this.currentPage,
          pageSize: this.pageSize,
          industryId: this.query.industryId,
          name: this.query.name,
          areaCode: this.query.areaCode,
          status: 3
        }
        getEnterpriseList(params).then(response => {
          this.loading = false
          this.gradeInfoList = response.data.data.dataList
          this.totalCount = response.data.data.total
        })

        getAreaCodeList().then(response => {
          console.log(response)
          this.areaCodeList = response.data
        })
      },

      searchGrade () {
        this.currentPage = 1
        this.loading = true
        this.getList()
      },

      changeSchoolTypeForQuery (val) {

      },

      // 选中阶段
      changeSchoolType (val) {

      },

      clearFormData () {
        this.form = {
          name: '',
          schoolType: ''
        }
      },

      deleteGradeInfo (enterprise) {
        this.$confirm('此操作将删除【' + enterprise.name + '】在系统中的所有数据,删除后不可恢复,是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.axios.delete(that.$http.httpUrl('/system/enterprise/' + enterprise.id))
            .then(response => {
              this.$message.success('删除成功')
              this.getList()
            })
        })
      },

      addGrade () {
        this.clearFormData()
        this.showFlag = true
      },

      handleSelectionChange (val) {

      },

      checkData (select) {
        console.log(select)
      },

      batchDelete () { // 批量删除

      },

      sizeChangeHandler (val) {
        this.pageSize = val
        this.currentPage = 1
        this.loading = true
        this.getList()
      },

      currentChangeHandler (val) {
        this.currentPage = val
        this.loading = true
        this.getList()
      },

      saveOrUpdate () {
        let that = this
        that.$refs['form'].validate(function (valid, rules) {
          if (valid) {
            that.axios.post(that.$http.httpUrl('/system/enterprise/saveOrUpdate'), that.form)
              .then(function (response) {
                that.showFlag = false
                if (response.data.code === 1) {
                  that.$message({
                    type: 'success',
                    message: response.data.message
                  })
                  that.clearFormData()
                  that.getList()
                }
              }).catch(function (error) {
                console.log(error)
              })
          }
        })
      },
      getStudentList (id) {
        this.axios.get(this.$http.httpUrl('/system/student'), {
          params: {
            enterpriseId: id,
            pageNumber: this.currentPage,
            pageSize: this.pageSize
          }
        }).then(response => {
          this.studentList = response.data.data.dataList
          this.totalCount = response.data.data.total
          this.loading = false
        })
      },
      staffInfo (item) {
        this.form = {
          id: item.id
        }
        this.getStudentList(item.id)
        this.satffFlag = true
      },
      // 注意 不能直接将item赋值给form
      updateGradeInfo (item) {
        this.form = {
          id: item.id,
          name: item.name,
          areaCode: item.areaCode,
          industryId: item.industryId
        }
        this.showFlag = true
      }

    },

    filters: {

      getSchoolTypeName (val) {
        for (let i in that.schoolTypeList) {
          let item = that.schoolTypeList[i]
          if (item.code === val) {
            return item.value
          }
        }
        return '无'
      }
    }
  }
</script>
<style>
  .avatar-uploader .el-upload {
    border: 1px dashed #d9d9d9;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }
  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 90px;
    height: 90px;
    line-height: 90px;
    text-align: center;
  }
  .avatar {
    width: 178px;
    height: 178px;
    display: block;
  }
</style>
